/**
 * Copyright 2012 Fonfon.Fenceer
 * All rights reserved.
 * 
 * @project 
 * @author Fonfon.Fenceer
 * @version 1.0
 * @data 2012-3-25
 */
package com.guidecaptain.java.action;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.guidecaptain.core.base.action.BaseAction;
import com.guidecaptain.java.model.pojo.Uploadify;
import com.guidecaptain.java.model.vo.UploadifyVO;
import com.guidecaptain.java.service.UploadService;

/**
 * 
 * @author fenceer
 */
@Controller
@Scope("prototype")
public class UploadAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger LOG = Logger.getLogger(UploadAction.class);

	@Autowired
	private UploadService uploadService;

	private UploadifyVO uploadifyVO = new UploadifyVO();

	/**
	 * 上传文件
	 */
	public String upload() {
		JSONObject jsonObject = new JSONObject();
		Uploadify uploadify = uploadService.insertUploadify(uploadifyVO);
		if (uploadify != null) {
			try {
				PropertyUtils.copyProperties(uploadifyVO, uploadify);
				jsonObject.put("filePath", uploadifyVO.getFilePath());
				jsonObject.put("fileName", uploadifyVO.getFileName());
			} catch (Exception e) {
				LOG.error(e);

				jsonObject.put("state", "fail");
			}

		} else {
			jsonObject.put("state", "fail");
		}

		setJsonObject(jsonObject);
		return JSON;
	}

	/**
	 * @param
	 * @return
	 */
	@Override
	public Object getModel() {

		return uploadifyVO;
	}

}
